using System;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization; 
using System.Xml.XPath; 

namespace XTCast.Radio
{
	/// <summary>
	/// Summary description for XMLConfigHandler.
	/// </summary>

	public class XMLConfigHandler : IConfigurationSectionHandler { 

		public object Create(object parent, object configContext, System.Xml.XmlNode section) { 
			try {
				XPathNavigator nav = section.CreateNavigator(); 
				string typename = System.Convert.ToString(nav.Evaluate("string(@type)")); 
				Type t = Type.GetType(typename); 
				if (t==null) throw new ConfigurationException(String.Format("Cannot find type '{0}'",typename));
				XmlSerializer ser = new XmlSerializer(t); 
				return ser.Deserialize(new XmlNodeReader(section)); 
			} catch (Exception ex) { 
				return ex;
			//	System.Diagnostics.Debug.WriteLine(ex);

				//throw ex; 
			}
		} 
	}
}
